﻿using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System;
using System.Collections.Generic;

namespace ExpertNewsRotator2
{
    public class ImageUploader : SPFieldText
    {
        private bool _IsNew;
        private Dictionary<string, string> CustomProperties;
        private static Dictionary<string, string> CustomPropertiesForNewFields = new Dictionary<string, string>();
        private static string[] CustomPropertyNames = new string[] { "MyCustomProperty", "PAWidth" };

        public ImageUploader(SPFieldCollection fields, string fieldName)
            : base(fields, fieldName)
        {
            this._IsNew = false;
            this.CustomProperties = new Dictionary<string, string>();
            this.InitProperties();
        }

        public ImageUploader(SPFieldCollection fields, string typeName, string displayName)
            : base(fields, typeName, displayName)
        {
            this._IsNew = false;
            this.CustomProperties = new Dictionary<string, string>();
            this.InitProperties();
        }

        private string GetCacheKey(string propertyName)
        {
            return string.Concat(new object[] { SPContext.Current.GetHashCode(), "_", (base.ParentList == null) ? "SITE" : base.ParentList.ID.ToString(), "_", propertyName });
        }

        public object GetCustomProperty(string propertyName)
        {
            if (!(this.IsNew || !CustomPropertiesForNewFields.ContainsKey(this.GetCacheKey(propertyName))))
            {
                string str = CustomPropertiesForNewFields[this.GetCacheKey(propertyName)];
                CustomPropertiesForNewFields.Remove(this.GetCacheKey(propertyName));
                this.CustomProperties[propertyName] = str;
                return str;
            }
            return this.CustomProperties[propertyName];
        }

        private void InitProperties()
        {
            foreach (string str in CustomPropertyNames)
            {
                this.CustomProperties[str] = (String)base.GetCustomProperty(str);
            }
        }

        public override void OnAdded(SPAddFieldOptions op)
        {
            base.OnAdded(op);
            this.Update();
        }

        private void SaveProperties()
        {
            foreach (string str in CustomPropertyNames)
            {
                base.SetCustomProperty(str, this.GetCustomProperty(str));
            }
        }

        public void SetCustomProperty(string propertyName, object propertyValue)
        {
            if (this.IsNew)
            {
                CustomPropertiesForNewFields[this.GetCacheKey(propertyName)] = (String)propertyValue;
            }
            this.CustomProperties[propertyName] = (String)propertyValue;
        }

        public override void Update()
        {
            this.SaveProperties();
            base.Update();
        }

        public override BaseFieldControl FieldRenderingControl
        {
            get
            {
                return new ImageUploaderControl(this) { FieldName = base.InternalName };
            }
        }

        public bool IsNew
        {
            get
            {
                return this._IsNew;
            }
            set
            {
                this._IsNew = value;
            }
        }

        public string MyCustomProperty
        {
            get
            {
                return ((String)this.GetCustomProperty("MyCustomProperty"));
            }
            set
            {
                this.SetCustomProperty("MyCustomProperty", value);
            }
        }

        public string PAWidth
        {
            get
            {
                return ((String)this.GetCustomProperty("PAWidth"));
            }
            set
            {
                this.SetCustomProperty("PAWidth", value);
            }
        }
    }
}
